/*
 * Copyright 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@file:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)

package androidx.compose.remote.creation.compose.modifier

import androidx.annotation.RestrictTo
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.remote.core.operations.layout.modifiers.DimensionModifierOperation.Type
import androidx.compose.remote.creation.compose.state.RemoteDp
import androidx.compose.remote.creation.compose.state.RemoteFloat
import androidx.compose.remote.creation.modifiers.RecordingModifier
import androidx.compose.runtime.Composable

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class HeightModifier(public val type: Type, public val value: RemoteFloat) :
    RemoteModifier.Element {
    override fun toRemoteComposeElement(): RecordingModifier.Element {
        return androidx.compose.remote.creation.modifiers.HeightModifier(
            type,
            value.internalAsFloat(),
        )
    }
}

public fun RemoteModifier.height(height: RemoteDp): RemoteModifier =
    then(HeightModifier(Type.EXACT_DP, height.value))

public fun RemoteModifier.height(height: RemoteFloat): RemoteModifier =
    then(HeightModifier(Type.EXACT, height))

public fun RemoteModifier.fillMaxHeight(fraction: RemoteFloat = RemoteFloat(1f)): RemoteModifier =
    then(HeightModifier(Type.FILL, fraction))

public fun RemoteModifier.fillMaxHeight(fraction: Float): RemoteModifier =
    then(HeightModifier(Type.FILL, RemoteFloat(fraction)))

@Composable
public fun RemoteModifier.height(height: Int): RemoteModifier =
    then(HeightModifier(Type.EXACT, RemoteFloat(height.toFloat())))

@Composable
public fun RemoteModifier.height(height: IntrinsicSize): RemoteModifier {
    return if (height == IntrinsicSize.Min) {
        then(HeightModifier(Type.INTRINSIC_MIN, RemoteFloat(0f)))
    } else {
        then(HeightModifier(Type.INTRINSIC_MAX, RemoteFloat(0f)))
    }
}
